#!/usr/bin/python
# Converts all .amr files in a directory to .wav

import sys
from os import listdir, remove, path
from subprocess import check_call

def amr_to_wav(directory):
    amr_files = filter(lambda f: f.endswith(".amr"), listdir(directory))
    for amr_file in amr_files:
        amr_file = path.join(directory, amr_file)
        wav_file = amr_file.replace(".amr", ".wav")
        check_call(["ffmpeg", "-i", amr_file, wav_file])
        remove(amr_file)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("\nConverts .amr files to .wav. Requires ffmpeg.")
        print("Usage: ./convert.py directory")
        print("  directory: path to directory containing amr files\n")
    else:
        directory = sys.argv[1]
        amr_to_wav(directory)

